function  [s,ds]=energy(tt,U,delta_U,n)  
%-\int 0.5 (-\delta u(x)+a_t(x) u(x))u(x)-sf(u(x)) dx = negative energy in PDE
%reversed the sign for minimization.for minimization 

global c a_t b p e t n_t n_p x_t y_t ar nth0 pwr;

%u=zeros(n_t,1);
%delta_u=zeros(n_t,1);
%for i=1:n
%  u=u+tt(i)*U(:,i);
%  delta_u=delta_u+tt(i)*delta_U(:,i);
%end

u=U*tt;
delta_u=delta_U*tt;

[fu,dfu,sfu]=nonlinear_terms(u);

s=ar*((1/c*delta_u-a_t.*u).*u/2+sfu);
for i=1:n
  ds(i)=ar*((1/c*delta_u-a_t.*u+fu).*U(:,i)); %For minimization
end
